<!-- html -->
<template>
	<view class="container">
		<u-navbar title="壁纸" backIconColor="#fff" titleColor="#fff" :background="background"></u-navbar>
		<view class="left">

			<u-lazy-load v-for="(item,index) in leftList" :key="index" :image="item.image" :loading-img="loadingImg"
				:error-img="errorImg" @click="detail(item.image)"></u-lazy-load>
			<!-- <image v-for="(item,index) in leftList" :key="index" :src="item.image" alt="" mode="widthFix"
				@click="detail(item.image)"></image> -->
		</view>
		<view class="right">
			<u-lazy-load v-for="(item,index) in rightList" :key="index" :image="item.image" :loading-img="loadingImg"
				:error-img="errorImg" @click="detail(item.image)"></u-lazy-load>
			<!-- <image v-for="(item,index) in rightList" :key="index" :src="item.image" alt="" mode="widthFix"
				@click="detail(item.image)"></image> -->
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				// 图片列表
				list: [],
				loadingImg: '/static/loading.png',
				errorImg: '/static/loading-error.jpg',
				// 初始化左右盒子
				leftList: [],
				rightList: [],
				// 初始化左右盒子高度
				leftH: 0,
				rightH: 0,
				page: 1,
				pageSize: 10,
				isRequesting: false,
				background: this.TopColor,
				backTextColor: {
					'color': '#ffffff'
				},
				interstitialAd: null
			}
		},
		onLoad() {
			this.page = 1;
			this.leftList = [];
			this.rightList = [];
			this.list = [];
			this.doList()
			// 在页面onLoad回调事件中创建插屏广告实例
			if (wx.createInterstitialAd) {
				this.interstitialAd = wx.createInterstitialAd({
					adUnitId: '你自己的广告id'
				})
				this.interstitialAd.onLoad(() => {
					console.log("广告加载成功")
					if (this.interstitialAd) {
						this.interstitialAd.show().catch((err) => {
							console.error(err)
						})
					};
				})
				this.interstitialAd.onError((err) => {
					console.log("广告加载失败")
				})
				this.interstitialAd.onClose(() => {})
			}
		},
		onShow() {

		},
		methods: {
			detail(img) {
				this.$common.navigateTo('/pages/index/detail?img=' + img)
			},
			async doList() {
				if (this.isRequesting) {
					return;
				}
				this.isRequesting = true
				// console.log(this.page)
				const that = this
				try {
					let wallpaperPromise = await this.$http.post("/wallpaper/getWallpaperList", {
						"pageSize": this.pageSize,
						"pageNum": this.page
					});
					let wallpaper = await wallpaperPromise;
					const imagesList = wallpaper.data.list;
					console.log(wallpaper)
					if (imagesList.length == 0) {
						this.$common.errorToShow("没有更多壁纸了，呜呜呜！")
						return;
					}
					this.page++;
					const images = imagesList.forEach((item) => {
						item.image = item.url
						this.list.push(item)
					})
				} catch (e) {
					console.log(e)
				}
				console.log("list", this.list)
				for (let i = 0; i < this.list.length; i++) {
					let res = this.list[i];
					// 获取图片宽高
					uni.getImageInfo({
						src: res.image,
						success: (image) => {
							console.log(res.image)
							// 计算图片渲染高度
							let showH = (50 * image.height) / image.width
							// 判断左右盒子高度
							if (that.leftH <= that.rightH) {
								that.leftList.push(res)
								that.leftH += showH
							} else {
								that.rightList.push(res)
								that.rightH += showH
							}

						}
					})
				}
				this.isRequesting = false
				//清空
				this.list = []
			},
			onReachBottom() {
				this.doList();
			},
			onPageScroll(e) {
				this.scrollTop = e.scrollTop;
			},
			onPullDownRefresh() {
				console.log("下拉")
				this.page = 1
				this.list = []
				this.leftList = []
				this.rightList = []
				this.doList()
				// 执行下拉刷新时需要执行的代码
				uni.stopPullDownRefresh()
			}
		}
	}
</script>
<style lang="scss">
	.right,
	.left {
		display: inline-block;
		width: 49%;
		vertical-align: top;
	}

	.left {
		margin-right: 2%;
	}

	.left image,
	.right image {
		width: 100%;
		margin-bottom: 10rpx;
	}
</style>